package cn.anc.aonicardv.module.adpter.my;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.anc.aonicardv.R;
import cn.anc.aonicardv.bean.LanguageSwitchBean;

/**
 * function :语言选择
 * </p>
 * Created by llc on 2018/9/18.
 */
public class SwitchLanguageAdapter extends BaseAdapter {
    private Context mContext;
    private List<LanguageSwitchBean> mDataList = new ArrayList<>();
    private int selectPosition = -1;

    public SwitchLanguageAdapter(Context context) {
        this.mContext = context;
    }

    /**
     * 刷新适配器数据列表
     *
     * @param dataList 待添加的数据列表
     */
    public void refreshDataList(List<LanguageSwitchBean> dataList) {
        if (dataList != null) {
            mDataList.clear();
            mDataList.addAll(dataList);
            notifyDataSetChanged();
        }
    }

    public int getSelectPosition(){
        return selectPosition;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public LanguageSwitchBean getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_switch_language, null);
            viewHolder = new ViewHolder();
            viewHolder.mTvItemLeftText = convertView.findViewById(R.id.tv_item_language_name);
            viewHolder.mIvtemSelect = convertView.findViewById(R.id.iv_language_select);
            viewHolder.mVItemLine = convertView.findViewById(R.id.v_item_line);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mTvItemLeftText.setText(mDataList.get(position).getName());
        if (mDataList.get(position).isSelect()) {
            selectPosition = position;
            viewHolder.mIvtemSelect.setImageResource(R.mipmap.icon_selected);
        } else {
            viewHolder.mIvtemSelect.setImageResource(R.mipmap.icon_unselect);
        }
        if (position <mDataList.size()&&position == mDataList.size()-1) {
            viewHolder.mVItemLine.setVisibility(View.GONE);
        } else {
            viewHolder.mVItemLine.setVisibility(View.VISIBLE);
        }

        return convertView;
    }


    class ViewHolder {
        private TextView mTvItemLeftText;
        private ImageView mIvtemSelect;
        private View mVItemLine;
    }
}
